package com.dhcc.DRGsHosp.application.impl;

import java.util.List;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.openkoala.koalacommons.resourceloader.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.STDApplication;

/**
 * @title		:STDApplicationImpl.java
 * @description	:
 * @author		:lileizheng
 * @date		:2015年8月3日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Named
@Transactional
public class STDApplicationImpl implements STDApplication {

    private EntityRepository repository;

	private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}

	@Override
	public List<Object[]> getAllFee(String drgCode) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select avg(costamount), dcc_code from DRGs.t_Medicalrecord a \n ");
		sql.append(" left join DRGs.t_patientdrg b \n ");
		sql.append(" on a.id=b.parid \n ");
		sql.append(" left join DRGs.t_cost c \n ");
		sql.append(" on a.id=c.parid \n ");
		sql.append(" left join DRGs.t_costsub d \n ");
		sql.append(" on d.cost_sub_code=c.parcost_sub_code \n ");
		sql.append(" where a.out_date between to_date('2014-01-01', 'yyyy-mm-dd') and to_date('2014-12-31', 'yyyy-mm-dd') \n ");
		if(StringUtils.hasLength(drgCode)){
			sql.append(" and b.pardrg_code='"+drgCode+"' \n ");
		}
		sql.append(" and dcc_code is not null \n ");
		sql.append(" group by dcc_code \n ");
		List<Object[]> resultLs = getRepository().createSqlQuery(sql.toString()).list();
		
		return resultLs;
	}

	@Override
	public List<Object> getAddDRGs() {
		StringBuilder sql = new StringBuilder();
		sql.append(" select b.pardrg_code from DRGs.t_Medicalrecord a \n ");
		sql.append(" left join DRGs.t_patientdrg b \n ");
		sql.append(" on a.id=b.parid \n ");
		sql.append(" where a.out_date between to_date('2014-01-01', 'yyyy-mm-dd') and to_date('2014-12-31', 'yyyy-mm-dd') \n ");
		sql.append(" group by b.pardrg_code \n ");
		List<Object> resultLs = getRepository().createSqlQuery(sql.toString()).list();
		
		return resultLs;
	}

	@Override
	public List<Object[]> getAllQuotas() {
		StringBuilder sql = new StringBuilder();
		sql.append(" select avg(costamount) avgFee, avg(in_days) avgDays, \n ");
		sql.append(" avg(drugcost) drugFee, avg(drugcost)/avg(costamount) drugRatio,  \n ");
		sql.append(" avg(materialcost) materialFee, avg(materialcost)/avg(costamount) materialRatio, \n ");
		sql.append(" count(1) totalCaseCnt,  \n ");
		sql.append(" sum(decode(out_way, 5, 1, 0)) deadCaseCnt, b.pardrg_code \n ");
		sql.append(" from DRGs.t_Medicalrecord a \n ");
		sql.append(" left join DRGs.t_patientdrg b \n ");
		sql.append(" on a.id=b.parid \n ");
		sql.append(" where a.out_date between to_date('2014-01-01', 'yyyy-mm-dd') and to_date('2014-12-31', 'yyyy-mm-dd') \n ");
		sql.append(" and b.pardrg_code is not null \n ");
		sql.append(" group by b.pardrg_code \n ");
		List<Object[]> resultLs = getRepository().createSqlQuery(sql.toString()).list();
		return resultLs;
	}
}
